package com.zxbg.service.impl.systemtoolsServiceImpl;

import com.zxbg.dao.EmployeeDao;
import com.zxbg.dao.ErrorPageDao;
import com.zxbg.domain.ErrorPage;
import com.zxbg.service.systemtoolsService.BusinessManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BusinessManagementServiceImpl implements BusinessManagementService {
    @Autowired
    ErrorPageDao errorPageDao;
    @Autowired
    OrganizationalStructureServiceImpl organizationalStructureService;
    @Autowired
    EmployeeDao employeeDao;

    @Override
    public List<ErrorPage> getErrorPageInfo(String error_type, Integer tenant_id) {
        List<ErrorPage> tempList = new ArrayList<>();
        if(error_type.equals("all")){
            tempList = errorPageDao.getAllErrorPageInfo(tenant_id);
        } else {
            tempList = errorPageDao.getErrorByErrorType(error_type, tenant_id);
        }
        for (ErrorPage errorPage : tempList) {
            errorPage.setUpdater(organizationalStructureService.getAllKindsDept(employeeDao.getEmployeeByEmployeeId(errorPage.getUpdater_id(), tenant_id)));
        }
        return tempList;
    }

    @Override
    public String addErrorPage(ErrorPage errorPage, Integer tenant_id, String employee_id) {
        if(errorPageDao.getErrorByErrorType(errorPage.getError_type(), tenant_id).size() > 0) {
            return "已存在该错误类型";
        }
        errorPage.setTenant_id(tenant_id);
        errorPage.setUpdater_id(employee_id);
        errorPageDao.addErrorPage(errorPage);
        return "";
    }

    @Override
    public String updateErrorPage(ErrorPage errorPage, String employee_id) {
        List<ErrorPage> errorPageList = errorPageDao.getErrorByErrorType(errorPage.getError_type(), errorPage.getTenant_id());
        if(errorPageList.size() != 0 && !errorPageList.get(0).getError_type().equals(errorPage.getError_type())) {
            return "已存在该错误类型";
        }
        errorPage.setUpdater_id(employee_id);
        errorPageDao.updateErrorPage(errorPage);
        return "";
    }

    @Override
    public boolean deleteErrorPage(String error_type, Integer tenant_id) {
        return errorPageDao.deleteErrorPage(error_type, tenant_id) > 0;
    }
}
